package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.mapper.SystemDictionaryItemMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.SystemDictionaryQuery;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemDictionaryItemServiceImpl implements ISystemDictionaryItemService {

    @Autowired
    private SystemDictionaryItemMapper systemDictionaryItemMapper;

    @Override
    public void delete(Long id) {
        systemDictionaryItemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(SystemDictionaryItem systemDictionaryItem) {
        //判断用户有没有指定序号：没有序号自动追加
        if(systemDictionaryItem.getSequence() == null){
            int max = systemDictionaryItemMapper.selectMaxSequenceByParentId(systemDictionaryItem.getParentId());
            systemDictionaryItem.setSequence(max + 1);
        }
        systemDictionaryItemMapper.insert(systemDictionaryItem);
    }

    @Override
    public SystemDictionaryItem get(Long id) {
        return systemDictionaryItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SystemDictionaryItem> listAll() {
        return systemDictionaryItemMapper.selectAll();
    }

    @Override
    public void update(SystemDictionaryItem systemDictionaryItem) {
        systemDictionaryItemMapper.updateByPrimaryKey(systemDictionaryItem);
    }

    /**
     *
     * @param qo
     * @return
     */
    @Override
    public PageInfo query(SystemDictionaryQuery qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"sequence asc");//第三个参数：排序规则（序号升序）

        //明细的显示：不点击目录就不显示数据:通过目录的id值来判断
        if(qo.getParentId() != -1L){
            //查询结果集
            List<SystemDictionaryItem> list = systemDictionaryItemMapper.selectForList(qo);
            //return new PageResult(qo.getCurrentPage(), count, list, qo.getPageSize());
            return new PageInfo(list);
        }
        return null;
    }

    @Override
    public List<SystemDictionaryItem> selectMsgByDicSn(String sn) {
        return systemDictionaryItemMapper.selectMsgByDicSn(sn);
    }

    @Override
    public List<SystemDictionaryItem> selectTitle(Long[] titleIds) {
        return systemDictionaryItemMapper.selectTitle(titleIds);
    }
}
